package com.aier.admin_new.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.eicp.ghostboy.futil.MD5Util;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin_new.entity.RoleUsers;
import com.aier.admin_new.service.IMessageNService;
import com.aier.admin_new.service.IRoleService;
import com.aier.classes.entity.ClassParent;
import com.aier.classes.entity.ClassTeacher;
import com.aier.classes.entity.Classes;
import com.aier.classes.service.IClassService;
import com.aier.classes.service.IClassUserService;
import com.aier.common.common.CommonSession;
import com.aier.school.entity.School;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.service.ISchoolService;
import com.aier.school.service.ISchoolTeacherService;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("adminLoginNewAction")
public class AdminLoginNewAction extends AdminNewBaseAction {
	private static final long serialVersionUID = 1L;
	/**
	 * 管理员登陆
	 */
	// 账号
	public String account;
	// 密码
	public String pwd;
	private String phone_imei;  // 手机唯一标示符
	private int s; 				// 成功:1 ,失败:0
	private String error_info;  // 错误提示信息  s
	private Long uid;			//用户ID
	private String appUid;		//推送Uid

	@Resource(name = "userService")
	private IUserService userService;

	@Resource(name = "schoolService")
	private ISchoolService schoolService;

	@Resource(name = "classService")
	private IClassService classService;
	@Resource(name = "schoolTeacherService")
	private ISchoolTeacherService schoolTeacherService;
	@Resource(name = "classUserService")
	private IClassUserService classUserService;
    @Resource(name="messageNService")
    private IMessageNService messageNService;
    @Resource(name="roleService")
    private IRoleService roleService;

	private User user;
	private List<School> schoolList; // 用户所在学校
	private List<Classes> classList; // 用户所在班级
	private String appStatus;// APP登录时候的身份 1 校长级别的管理员 ，2 班级管理员  3 普通老师   4 家长   5未确定身份的用户
	private int sid;//学校id
	private Long  tid;//当前用户所在学校的tid
	private int counts;//记录下有几种身份

	@Override
	public String execute() {
		return SUCCESS;
	}		
	
	/**
	 * 执行后台管理员登陆
	 * 
	 * @return
	 */
	public String naLogin() {
		// 判断是否为学校的管理员用户 教师或者园长
		// 用户找到学校
		User user = userService.searchUserByCondition(account,MD5Util.getMD5(pwd));// 用户
		if (user == null) {
			s = 0;
			error_info = "用户名或者密码有误";
		} else {
			if (user.getIsbaby() == (short) 1) {
				s = 0;
				error_info = "用户名密码有误";
			} else {
				uid=user.getUid();				
				School sc = schoolService.searchSchoolByUid(user.getUid());
				sid=sc.getSid();
				classList = classService.searchClassListByUid(user.getUid());
				// 成功登录后记录session
                session.put(CommonSession.SESSION_ACCOUNT, user.getAccount());
                session.put(CommonSession.SESSION_ACCOUNT_ID, user.getUid());
                session.put(CommonSession.SESSION_ACCOUNT_NAME, user.getNickname());
                session.put(CommonSession.SESSION_ACCOUNT_SCHOOLLIST, schoolList);
                session.put(CommonSession.SESSION_ACCOUNT_CLASSLIST, classList);
                session.put(CommonSession.SESSION_USER_ROLE, appStatus);
				s = 1;
			}
		}
		return SUCCESS;
	}
	
	/**
	 *查询出用户的角色
	 *1	学校管理员
	 *2	班级管理员
 	 *3	考勤管理员
  	 *4	IC卡管理员
	 *5	普通教师
	 * @return
	 */
	public  String checkUserSturts(){
		
		User user = userService.searchUserByCondition(account);// 用户
		if (user == null) {
			s = 0;
			error_info = "用户不存在";
		}else{
			School sc = schoolService.searchSchoolByUid(user.getUid());
			classList = classService.searchClassListByUid(user.getUid());
			if(sc==null | classList.size()==0){
				s = 0;
				error_info = "您不是这个学校的老师.";
			}
			
            int sid=sc.getSid();
            uid=user.getUid();
			appStatus="";
			
			SchoolTeacher schoolTeacher = schoolTeacherService.searchSchoolTeacherByIds(uid, sid);
			if (schoolTeacher != null && (schoolTeacher.getIsadmin() == (short) 1)) { // 学校管理员
				  appStatus +="1,";		
			}
			
			for(Classes cs:classList){
				ClassTeacher classTeacher = classUserService.searchClassTeacherByIds(uid, cs.getCid());
				if (classTeacher != null && (classTeacher.getIsadmin() == (short) 1)) {// 班级管理员
					appStatus +="2,";
				}
			}
			
			List<RoleUsers> ru=roleService.searchRoleUsers(uid, sid);	
			for(RoleUsers rs: ru){
				int rid=rs.getRid();
				switch (rid) {
				case 3:
					appStatus+="3,";//考勤管理员
					break;
				case 4:
					appStatus+="4,";//IC卡管理员
					break;
				}
			}
			appStatus+="5,";//普通老师
			session.put("appStatus", appStatus);
			s = 1;
		}
		return SUCCESS;
	}

	
	/**
	 * 添加手机唯一标识
	 */
	public String add_imeis() {

		s = userService.updatePhoneImeiByUid(phone_imei, uid);

		return SUCCESS;
	}
	
	/**
	 * 添加手机Uid
	 */
	public String toAppUid() {
		s = userService.updateOrInsertAppUid(appUid, uid);
		return SUCCESS;
	}
	
	public String appnLogin() {		
		appStatus="0";
		phone_imei="1";
		user = userService.searchUserByCondition(account, MD5Util.getMD5(pwd));
		if (user == null) {
			s = 0;
			error_info = "用户名密码有误";
		} else {
			if (user.getIsbaby() == (short) 1) {
				s = 0;
				error_info = "用户名密码有误";
			} else {
				s = 1;
				schoolList = schoolService.searchSchoolListByUid(user.getUid());
				classList = classService.searchClassListByUid(user.getUid());	
				//更新用户在线状态
				userService.insertUserOnline(user.getUid());
				
				//与aier360团队建立会话
					int istrue= messageNService.searchFlter(user.getUid());
					if(istrue==0){
					 messageNService.insertMessage(user.getUid(),1801L, (short)4, "希望您能对我们的软件提出宝贵的意见!", (short)0, "");
					}
				
				if((schoolList.size()==0 && classList.size()==0 )){
					appStatus="5";// 未确定身份
				}
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 执行APP理员登陆
	 * @return
	 */
	public String appnLoginIOS() {
		appStatus="0";
		phone_imei="1";
		counts=0;
		user = userService.searchUserByCondition(account, MD5Util.getMD5(pwd));
		if (user == null) {
			s = 0;
			error_info = "用户名密码有误";
		} else {
			if (user.getIsbaby() == (short) 1) {
				s = 0;
				error_info = "用户名密码有误";
			} else {
				s = 1;
				List<School>  sList = schoolService.searchSchoolListByUid(user.getUid());
				List<Classes> cList = classService.searchClassListByUid(user.getUid());
				schoolList = new ArrayList<School>();				
				classList  = new ArrayList<Classes>();
				
				//更新用户在线状态
				userService.insertUserOnline(user.getUid());
				
				//与aier360团队建立会话
				int istrue= messageNService.searchFlter(user.getUid());
				if(istrue==0){
				 messageNService.insertMessage(user.getUid(),1801L, (short)4, "希望您能对我们的软件提出宝贵的意见!", (short)0, "");
				}
				
				//start
				if(sList.size()>0){
					for(School sc : sList ){
						SchoolTeacher schoolTeacher = schoolTeacherService.searchSchoolTeacherByIds(user.getUid(), sc.getSid());
						if(schoolTeacher != null ){
							tid=schoolTeacher.getTid();
							counts=counts+1;
							if ((schoolTeacher.getIsadmin() == (short) 1)) { // 学校管理员	
								sc.setAppStatusSchool("1");
							}else{
								sc.setAppStatusSchool("0");
							}
						}
						
						if(cList.size()>0){
							for(Classes ca : cList){
								ClassTeacher classTeacher = classUserService.searchClassTeacherByIds(user.getUid(), ca.getCid());
								if (classTeacher != null && (classTeacher.getIsadmin() == (short) 1)) {// 班级管理员
									ca.setAppStatusClass("2");
									counts=counts+1;
								}else{
									ca.setAppStatusClass("3");
									counts=counts+1;
								}
								classList.add(ca);
							}
						}
						
						List<ClassParent> classaist = classUserService.searchClassParentListByUid(user.getUid());
						if (classaist.size()>0) {
							Classes cas = new Classes();
							for(ClassParent cp :classaist){
								cas =classService.searchClassById(cp.getCid());
								cas.setAppStatusClass("4");
								counts=counts+1;
								}
							classList.add(cas);
						}
						sc.setClassList(classList);
						
						schoolList.add(sc);
					}
				}
				//end
				
				if((schoolList.size()==0 && classList.size()==0 )){
					appStatus="5";// 未确定身份
				}
				
			}
		}
		return SUCCESS;
	}	
	
	

	/**
	 * 切换身份时获取学校和班级列表
	 * @return
	 */
	public String appgetSchoolList() {
		s = 1;
		user=userService.searchBabyByUserid(uid);
		schoolList = schoolService.searchSchoolListByUid(uid);
		classList = classService.searchClassListByUid(uid);
		//start
		if(schoolList.size()>0){
			for(School sc : schoolList ){
				SchoolTeacher schoolTeacher = schoolTeacherService.searchSchoolTeacherByIds(user.getUid(), sc.getSid());
				if(schoolTeacher != null ){
					tid=schoolTeacher.getTid();
					counts=counts+1;
					if ((schoolTeacher.getIsadmin() == (short) 1)) { // 学校管理员	
						sc.setAppStatusSchool("1");
					}else{
						sc.setAppStatusSchool("0");
					}
				}
				
				if(classList.size()>0){
					for(Classes ca : classList){
						ClassTeacher classTeacher = classUserService.searchClassTeacherByIds(user.getUid(), ca.getCid());
						if (classTeacher != null && (classTeacher.getIsadmin() == (short) 1)) {// 班级管理员
							ca.setAppStatusClass("2");
							counts=counts+1;
						}else{
							ca.setAppStatusClass("3");
							counts=counts+1;
						}
					}
				}
				
				List<ClassParent> classaist = classUserService.searchClassParentListByUid(user.getUid());
				if (classaist.size()>0) {
					Classes cas = new Classes();
					for(ClassParent cp :classaist){
						cas =classService.searchClassById(cp.getCid());
						cas.setAppStatusClass("4");
						counts=counts+1;
						}
				}
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 用户离线
	 * @return
	 */
	public String appLogout(){
			s=userService.updateUserOnline(uid);
		return SUCCESS;
	}
	public void setAccount(String account) {
		this.account = account;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public int getS() {
		return s;
	}

	public String getError_info() {
		return error_info;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public void setSchoolTeacherService(
			ISchoolTeacherService schoolTeacherService) {
		this.schoolTeacherService = schoolTeacherService;
	}

	public void setClassUserService(IClassUserService classUserService) {
		this.classUserService = classUserService;
	}

	public void setSchoolService(ISchoolService schoolService) {
		this.schoolService = schoolService;
	}

	public void setClassService(IClassService classService) {
		this.classService = classService;
	}

	public List<School> getSchoolList() {
		return schoolList;
	}

	public List<Classes> getClassList() {
		return classList;
	}	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getAppStatus() {
		return appStatus;
	}

	public void setAppStatus(String appStatus) {
		this.appStatus = appStatus;
	}

	public void setAppUid(String appUid) {
		this.appUid = appUid;
	}

	public Long getUid() {
		return uid;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}

	public int getSid() {
		return sid;
	}

	public Long getTid() {
		return tid;
	}

	public int getCounts() {
		return counts;
	}
}
